//  时间转换器
//  参数传递 time是时间戳， 传递judge年月日时分秒的首字母就是需要展示的时间格式
let timeCycle = (time,judge) => {
    if (time) {
        var str = "",
            date = new Date(time),
            year = date.getFullYear(), //获取系统的年；
            month = date.getMonth() + 1, //获取系统月份，由于月份是从0开始计算，所以要加1
            day = date.getDate(), // 获取系统日，
            hour = date.getHours(), //获取系统时，
            minute = date.getMinutes(), //分
            second = date.getSeconds(); //秒
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        if (hour < 10) {
            hour = "0" + hour;
        }
        if (minute < 10) {
            minute = "0" + minute;
        }
        if (second < 10) {
            second = "0" + second;
        }

        if (judge === 'ymdhms') { // 传递年月日时分秒的首字母就是需要展示的时间格式
            str = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
        } else if (judge === 'ymd') {
            str = year + '-' + month + '-' + day; 
        } else if (judge === 'hms') {
            str = hour + ':' + minute + ':' + second;
        } else if (judge === 'hm') {
            str = hour + ':' + minute;
        } else if (judge === 'ymdhm') {
            str = year + '-' + month + '-' + day + ' ' + hour + ':' + minute; 
        }
    } else {
        str = '传递的参数不是时间戳'
    }
    return str;
}
// 时间转换器把时分秒转换成总秒数
let timeTransverter = (time) => {
    var s = '';
    if (time.length === 8) {     // 12:00:00
        var hour = time.split(':')[0];
        var min = time.split(':')[1];
        var sec = time.split(':')[2];
        s = Number(hour*3600) + Number(min*60) + Number(sec);
    } else if (time.length === 5) {  // 01:00
        var min = time.split(':')[0];
        var sec = time.split(':')[1];
        s = Number(min*60) + Number(sec);
    } else {
        s = '时间格式错误'
    }
        return s;
}

// 以中文形式展示时间戳
let formatMsgTime = (argument) => {
    var oldTime = argument
    var dataTime = (date) => {
        if (date) {
            var str = "",
                date = new Date(date),
                year = date.getFullYear(), //获取系统的年；
                month = date.getMonth() + 1, //获取系统月份，由于月份是从0开始计算，所以要加1
                day = date.getDate(), // 获取系统日，
                hour = date.getHours(), //获取系统时，
                minute = date.getMinutes(), //分
                second = date.getSeconds(); //秒
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            if (hour < 10) {
                hour = "0" + hour;
            }
            if (minute < 10) {
                minute = "0" + minute;
            }
            if (second < 10) {
                second = "0" + second;
            }
    
            str = year + '年' + month + '月' + day + '日   ' + hour + ':' + minute + ':' + second;
        } else {
            str = '传递的参数不是时间戳'
        }
        return str;
    }
    let getTimeS = (argument) => {
        var timeS = argument
        timeS = timeS.replace(/[年月]/g,'/').replace(/[日]/,'')
        return new Date().getTime() - new Date(timeS).getTime() - 1000; //有一秒的误差
    }
    var argument = dataTime(argument)
    var timeS = argument;  
    var todayT = '';
    var yestodayT = '';  
    var timeCha = getTimeS(timeS)
    timeS = timeS.slice(-8);  
    todayT = new Date().getHours()*60*60*1000 + new Date().getMinutes()*60*1000 + new Date().getSeconds()*1000;  
    yestodayT = todayT + 24*60*60*1000;  
    if(timeCha > yestodayT) {  
        return argument.slice(0,11) + '  ' + timeCycle(oldTime, 'hm');  
    }  
    if(timeCha > todayT && timeCha < yestodayT) {  
        return timeS.slice(0,2)>12?'昨天 下午'+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):'昨天 上午'+timeS.slice(0,5);  
    }  
    if(timeCha < todayT) {  
        return timeS.slice(0,2)>=12?'下午  '+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):'上午  '+timeS.slice(0,5);  
    }  
}
export { 
    timeCycle,
    timeTransverter,
    formatMsgTime
}